/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.teradata.olingo.provider;

import com.teradata.olingo.service.EdmTypeTrnsfService;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.provider.CsdlEdmProvider;
import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainer;
import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainerInfo;
import org.apache.olingo.commons.api.edm.provider.CsdlEntitySet;
import org.apache.olingo.commons.api.ex.ODataException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

import static com.teradata.olingo.constant.OdataConst.CONTAINER_NAME;
import static com.teradata.olingo.constant.OdataConst.NAMESPACE;
public class GenericContainerProvider {

  @Autowired
  EdmTypeTrnsfService edmTypeTrnsfService;
  private FullQualifiedName CONTAINER;


  public GenericContainerProvider() {
    super();
    CONTAINER = new FullQualifiedName(NAMESPACE, CONTAINER_NAME);
  }
  public CsdlEntityContainerInfo getEntityContainerInfo(FullQualifiedName entityContainerName) throws ODataException {
    // This method is invoked when displaying the service document at
    // e.g. http://localhost:8080/DemoService/DemoService.svc
    if (entityContainerName == null || entityContainerName.equals(CONTAINER)) {
      CsdlEntityContainerInfo entityContainerInfo = new CsdlEntityContainerInfo();
      entityContainerInfo.setContainerName(CONTAINER);
      return entityContainerInfo;
    }
    return null;
  }

  public CsdlEntityContainer getEntityContainer() throws ODataException {
    // create EntitySets
    List<CsdlEntitySet> entitySets = new ArrayList<CsdlEntitySet>();

    for (String apiName : edmTypeTrnsfService.getMetadataFromDB().keySet()) {
      entitySets.add(getEntitySet(CONTAINER, apiName));
    }

    // create EntityContainer
    CsdlEntityContainer entityContainer = new CsdlEntityContainer();
    entityContainer.setName(CONTAINER_NAME);
    entityContainer.setEntitySets(entitySets);

    return entityContainer;
  }

  public CsdlEntitySet getEntitySet(FullQualifiedName entityContainer, String entitySetName) throws ODataException {
    if (entityContainer.equals(CONTAINER)) {
      CsdlEntitySet entitySet = new CsdlEntitySet();
      entitySet.setName(entitySetName);
      entitySet.setType(new FullQualifiedName(NAMESPACE, entitySetName));
      return entitySet;
    }

    return null;
  }
}
